<template>
    <div>
                <b>角色:</b> <el-select @change="setCheckedKeys" v-model="roleName" clearable placeholder="请选择">
                <el-option
                        v-for="item in options"
                        :key="item.id"
                        :label="item.roleName"
                        :value="item.id">
                </el-option>
            </el-select>
        <el-button @click="getCheckedKeys">授权</el-button>
        <br> <br>
    <el-tree
            v-loading="loading"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.8)"
            style="width: 100%"
            :data="data"
            show-checkbox
            :default-expand-all="false"
            node-key="id"
            ref="tree"
            highlight-current
            :props="defaultProps">
    </el-tree>

    <div class="buttons">
<!--        <el-button @click="getCheckedNodes">通过 node 获取</el-button>-->
<!--        <el-button @click="getCheckedKeys">通过 key 获取</el-button>-->
        <el-button @click="resetChecked">清空</el-button>
    </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                loading: true,
                data: [],
                defaultProps: {
                    children: 'childMenus',
                    label: 'mname'
                },
                options: [],
                roleName: '',
            }

        },
        methods: {
            getroles(){
                this.$axios.post("queryallroles.action").
                then(request=>{
                    this.options = request.data
                }).
                catch()
            },
            getmenus(){
                this.$axios.post("showmenusall.action").
                then(request=>{
                    this.data = request.data.filter(item=>{
                        return item.childMenus= item.childMenus.filter(item=>{
                            return item.mname!="权限管理"
                        })
                    })
                    this.loading=false
                    //this.data = request.data

                }).
                catch()
            },
            getCheckedKeys() {
                let menurole = new URLSearchParams()
                let rId =this.roleName
                let mId = this.$refs.tree.getCheckedKeys()
                menurole.append("rId",rId)
                menurole.append("listMid",mId)
                if (this.roleName!=""){
                    this.$axios.post("authoritySystem.action",menurole).
                    then(request=>{
                        if (request.data.msg=="授权成功"){
                            this.$message.success("授权成功")
                            this.roleName = ""
                            this.$refs.tree.setCheckedKeys([]);
                            this.getmenus()
                        }else {
                            this.$message.error("授权失败")
                        }

                    }).
                    catch()
                }else {
                    this.$message.warning("请选择角色")
                }


            },
            setCheckedKeys() {
                let rid = this.roleName
                let role = new URLSearchParams()
                role.append("id",rid)
                this.$axios.post("querySystembyrid.action",role).
                then(request=>{

                    let list = request.data.filter(item=>{
                        return item.parentId!=0 && item!=null
                    })
                    let listrole = []

                    list.forEach(item=>{
                        listrole.push(item.id)
                    })

                    let list2=[]
                    listrole.forEach((item)=>{
                        if (item!=null && item!=""){
                            list2.push(item);
                        }
                    })

                    this.$refs.tree.setCheckedKeys(list2);
                }).
                catch()

            },
            resetChecked() {
                this.$refs.tree.setCheckedKeys([]);
            }
        },created() {
            this.getroles(),
            this.getmenus()
        }
    };
</script>

<style scoped>

</style>